package com.tianlang.web.controller;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tianlang.domain.pojo.Account;
import com.tianlang.security.tool.WcContext;
import com.tianlang.service.IUserManager;
import com.tianlang.util.CookieUtil;
import com.tianlang.util.MD5;
import com.tianlang.util.StringUtil;

@Scope("prototype")
@Controller
public class LoginController {
	@Autowired
	private UserDetailsService userDetailsService;// org.springframework.security.core.userdetails
	@Autowired
	private IUserManager userManager;

	@RequestMapping("")
	public String Default() throws Exception {// 默认首页
		return "redirect:/index";
	}

	@RequestMapping("/login")
	public String index(HttpServletRequest request, Model model) throws Exception {
		// 记住用户名
		Cookie user = CookieUtil.getCookie(request, "tianlang_user");
		if (user != null) {
			if (StringUtil.isNotEmpty(user.getValue())) {
				Account account = new Account();
				account.setUsername(user.getValue());
				model.addAttribute("account", account);
			}
		}
		return "/login";
	}

	@RequestMapping(value = "/login/save", method = RequestMethod.POST)
	public String loginSave(Account account, Integer remember, Model model, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		UserDetails userDetails = userDetailsService.loadUserByUsername(account.getUsername());// 用于判断用户是否存在
		if (userDetails == null) {// 用户不存在 直接跳转回登录页面
			model.addAttribute("error", "没有这个帐号");
			model.addAttribute("account", account);
			return "/login";
		}
		Account _account = userManager.getAccountByName(account.getUsername());
		if (_account.getPassword().equals(MD5.md5(account.getPassword()))) {
			Authentication authentication = new UsernamePasswordAuthenticationToken(_account, "",
					userDetails.getAuthorities());
			SecurityContext securityContext = SecurityContextHolder.getContext();
			securityContext.setAuthentication(authentication);
			if (remember != null && remember == 1) {
				CookieUtil.setCookie(request, response, "tianlang_user", _account.getUsername());
			} else {
				Cookie cookie = CookieUtil.getCookie(request, "tianlang_user");
				CookieUtil.deleteCookie(request, response, cookie);
			}
			return "redirect:/index";
		} else {
			model.addAttribute("error", "密码不正确");
			model.addAttribute("account", account);
			return "/login";
		}
	}

	@RequestMapping("/logout")
	public String logout(HttpSession session) throws Exception {
		WcContext.setAuthentication(null);
		session.invalidate();
		return "redirect:/login";
	}

}
